package publish;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import contentSpider.ZHHant;

public class PublishContentDG {

	/**
	 * @param args
	 */
	public void publish() {
		System.setProperty("LOG_DIR", "./");
		Log logger = LogFactory.getLog(PublishContentDG.class);
		Connection conn = null;
		Connection connInsert = null;
		Statement stmt = null;
		ResultSet rs = null;
		Statement stmtUpdateTag = null;
		PreparedStatement pstmt = null;
		Statement stmtPostidContent = null;
		boolean alltime = true;
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		while (alltime) {
			try {
				conn = DriverManager.getConnection("jdbc:mysql://" + dbhost + "/" + dbname + "?user=root&password=777888999a!&useUnicode=true&characterEncoding=utf8");
				connInsert = DriverManager.getConnection("jdbc:mysql://" + hostWP + "/" + dbnameWP + "?user=root&password=777888999a!&useUnicode=true&characterEncoding=utf8");
				stmt = conn.createStatement();
				stmtUpdateTag = conn.createStatement();
				stmtPostidContent = conn.createStatement();
				// while(true){}
				rs = stmt.executeQuery("select * from sublist");
				ArrayList<String> subRexList = new ArrayList<String>();
				subRexList.add("0");
				while (rs.next()) {
					subRexList.add(rs.getString("description"));
				}

				rs = stmt.executeQuery("select id,title,content,title,subid from content where wyc = 0 and contentTW is not null and titleTW is not null limit 1000");
				// stmt.executeQuery("select id,titleTW,contentTW from content where copy = 1 and wyc = 0");
				while (rs.next()) {
					stmtUpdateTag.executeUpdate("update content set wyc = 1 where id = " + rs.getInt("id"));
					String title = rs.getString("title");
					String fileName = String.valueOf(rs.getInt("id"));
					String content = rs.getString("content").replaceAll(">\\s*", ">").replaceAll("\\s*<", "<");
					Pattern p = Pattern.compile(subRexList.get(rs.getInt("subid")));
					Matcher m = p.matcher(title);
					if (!m.find()) {
						continue;
					}
					/*
					 * if (title.length() > 35) { title = title.substring(0,
					 * 35); }
					 */
					if(fyHost.length()>8){
						ZHHant zht = new ZHHant();
						zht.setFyHost(fyHost);
						title = zht.tChinese(title);
						content = zht.tChinese(content);
						fileName = new SimpleDateFormat("ddhhmmssSSS").format(new Date());
					}
					pstmt = connInsert.prepareStatement("insert into " + TableMain + " (id,classid,onclick,userid,username,newstime,truetime,lastdotime,smalltext,filename,title,diggtop,stb) values ( ?, ? ,? ,?, ? ,? ,? ,? ,?,?,?,?,?)");
					pstmt.setInt(1, rs.getInt("id"));
					pstmt.setInt(2, rs.getInt("subid"));
					pstmt.setInt(3, new Random().nextInt(1000));
					pstmt.setInt(4, 1);
					pstmt.setString(5, "admin");
					pstmt.setLong(6, System.currentTimeMillis() / 1000);
					Long t1 = System.currentTimeMillis() / 1000;
					pstmt.setLong(7, t1);
					pstmt.setLong(8, t1);
					String jianjie = null;
					if (content.replaceAll("<.*?>", "").length() < 10) {
						jianjie = title;
					}
					if (content.replaceAll("<.*?>", "").length() > 9 && content.replaceAll("<.*?>", "").length() < 201) {
						jianjie = content.replaceAll("<.*?>", "");
					}
					if (content.replaceAll("<.*?>", "").length() > 200) {
						jianjie = content.replaceAll("<.*?>", "").substring(0, 200);
					}
					pstmt.setString(9, jianjie);
					pstmt.setString(10, fileName);
					pstmt.setString(11, title);
					pstmt.setInt(12, new Random().nextInt(100));
					pstmt.setInt(13, 1);
					pstmt.executeUpdate();
					pstmt = connInsert.prepareStatement("insert into " + ContentTable + "(id,classid,newstext) values (?,?,?)");
					pstmt.setInt(1, rs.getInt("id"));
					pstmt.setInt(2, rs.getInt("subid"));
					pstmt.setString(3, content);
					pstmt.executeUpdate();
					stmtPostidContent.execute("update content set postid = " + rs.getInt("id") + " where id = " + rs.getInt(1));
					System.out.println(rs.getInt("id"));
				}
				rs = stmt.executeQuery("select id from content where wyc = 0 and contentTW is not null and titleTW is not null");
				if (!rs.next()) {
					alltime = false;
				} else {
					continue;
				}

			} catch (SQLException ex) {
				ex.printStackTrace();
				continue;
			} finally {
				try {
					if (rs != null) {
						rs.close();
						rs = null;
					}
					if (stmt != null) {
						stmt.close();
						stmt = null;
					}
					if (conn != null) {
						conn.close();
						conn = null;
					}
					if (stmtUpdateTag != null) {
						stmtUpdateTag.close();
						stmtUpdateTag = null;
					}
					if (pstmt != null) {
						pstmt.close();
						pstmt = null;
					}
					if (connInsert != null) {
						connInsert.close();
						connInsert = null;
					}
					if (stmtPostidContent != null) {
						stmtPostidContent.close();
						stmtPostidContent = null;
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}

		}
	}

	public void setDbhost(String dbhost) {
		this.dbhost = dbhost;
	}

	public void setHostWP(String hostWP) {
		this.hostWP = hostWP;
	}

	public void setTableMain(String tableMain) {
		TableMain = tableMain;
	}

	public void setContentTable(String contentTable) {
		ContentTable = contentTable;
	}

	public void setDbname(String dbname) {
		this.dbname = dbname;
	}

	public void setDbnameWP(String dbnameWP) {
		this.dbnameWP = dbnameWP;
	}
	
	public void setFyHost(String fyHost) {
		this.fyHost = fyHost;
	}

	String fyHost = "";
	String dbhost = null;
	String hostWP = null;
	String TableMain = null;
	String ContentTable = null;
	String dbname = null;
	String dbnameWP = null;
}
